package com.frank.test;

import com.alibaba.fastjson.JSONObject;
import com.frank.demo.zookeeper.curator.zk.LockHandler;
import com.frank.demo.zookeeper.curator.zk.ZkStarter;
import org.junit.Test;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;

/**
 * Created by zhy on 2018/5/2.
 */
public class Zk {
    @Test
    public synchronized void startLock() throws Exception {
        new ZkStarter(new LockHandler("/zhy/lock", "127.0.0.1:2181", 2, new LockHandler.LockStart() {
            @Override
            public void onStart(JSONObject data) {
                System.out.println("Start...");
            }

            @Override
            public void onDestory() throws Exception {
                System.out.println("ShutDown...");
                System.exit(0);
            }
        })).startup();

        while (true) {
            TimeUnit.SECONDS.sleep(10000);
        }
    }


    @Test
    public void t() {

        queen(new int[]{1, 1, 1, 1, 1, 1, 1, 1}, 0);
    }

    private int i = 1;

    private void queen(int[] a, int cur) {
        if (cur == a.length) {
            System.out.println(i++ + "         " + Arrays.toString(a));
            return;
        }
        for (int i = 0; i < a.length; i++) {
            a[cur] = i;
            boolean flag = true;
            for (int j = 0; j < cur; j++) {
                int ab = i - a[j];
                if (ab == 0 || Math.abs(ab) == cur - j) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                queen(a, cur + 1);
            }
        }
    }
}
